Skip to content

[None][refactor] Unify sparse attention framework with clean backend interfaces#12733

Open
lfr-0531 wants to merge 11 commits into
NVIDIA:mainfrom
lfr-0531:user/fanrongl/sparse-attention-refactor
Open

[None][refactor] Unify sparse attention framework with clean backend interfaces#12733
lfr-0531 wants to merge 11 commits into
NVIDIA:mainfrom
lfr-0531:user/fanrongl/sparse-attention-refactor

Conversation

@lfr-0531

@lfr-0531 lfr-0531 commented Apr 3, 2026

Copy link
Copy Markdown
Collaborator

Dev Engineer Review

  • Major sparse-attention refactor around explicit contracts:
    • Introduced SparseBackendForwardArgs and SparseRuntimeParams, removing SparsePrediction and replacing SkipSoftmaxKernelParams usage with runtime fields.
    • Added sparse hook facade/dispatcher in tensorrt_llm/_torch/attention_backend/sparse/hooks.py, and rewired MLA/attention execution to route sparse execution through hooks and consume predicted indices from forward_args.sparse_runtime_params.*.
  • Split and reorganized sparse backends under tensorrt_llm/_torch/attention_backend/sparse/<algorithm>/:
    • DSA: new sparse/dsa/ package (backend, indexer, kernels, metadata, cache manager, custom CUDA-graph-friendly ops, params).
    • DeepSeek-V4: new sparse/deepseek_v4/ package (runtime-only metadata, backend/indexer/kernels/compressor, params).
    • RocketKV: migrated from sparse/rocket.py to sparse/rocket/ package; the monolithic rocket.py was deleted and replaced by package modules (backend/cache_manager/kernels/metadata/params).
  • Updated call sites to read/write sparse indices/offsets from SparseRuntimeParams across multiple backends and FMHA fallbacks, including SkipSoftmax threshold scaling fields.
  • Updated lint/config allowlists to match the new sparse package layout:
    • .pre-commit-config.yaml, legacy-files.txt, pyproject.toml, ruff-legacy.toml.
  • Notable correctness/performance risks to watch:
    • Triton/DSP boundary behavior: DSA kernels (e.g., global index conversion) only explicitly invalidate on some conditions (e.g., tok < 0, page-range check), not necessarily on all negative/out-of-range intermediates—verify assumptions about block_table/pool-page validity.
    • Cache/index lifecycle: DSA shared-topk lifecycle across on_update_kv_lens() and CUDA-graph capture paths via custom ops; ensure buffer object identity/stability and cache revalidation are consistent.
    • RocketKV migration/deletion: ensure all prior imports and expected symbols are correctly re-exported from the new sparse/rocket/ package and that any stale references to the removed rocket.py are gone.
    • Interface migration completeness: ensure every sparse consumer now correctly uses forward_args.sparse_runtime_params (not legacy prediction/kernel-param paths), especially where multiple FMHA/FlashInfer/MSA backends branch on “sparse vs dense plan” decisions.
  • CI/comments (from provided context): mixed outcomes with multiple pipelines failing and several helper jobs aborted/succeeded; failures likely require inspecting the specific sparse-attention tests/pipelines impacted by the refactor.

QA Engineer Review

No test changes (no tests/, tests/integration/test_lists/, test-db/, qa/, or waives.txt modifications detected in the diff evidence available here).

Description

TensorRT-LLM already provides a sparse-attention framework for DSA, DeepSeek-V4,
RocketKV, and SkipSoftmax, but algorithm-specific module, backend, prediction,
metadata, cache, and kernel logic had become mixed across the common Attention
and MLA paths. That made the extension boundary inconsistent and required
changes to shared modules when adding or maintaining an algorithm.

This PR refactors sparse attention around explicit module and backend contracts:

  • Attention and MLA remain the shared modules. They resolve a validated set of
    optional sparse-attention hooks for module initialization, weights, forward,
    custom-op execution, and output projection.
  • Algorithm-specific module hooks and implementations live under
    _torch/attention_backend/sparse/<algorithm>/.
  • SparseBackendForwardArgs carries sparse inputs from Attention/MLA to the
    selected backend, while SparseRuntimeParams carries prediction and
    SkipSoftmax runtime inputs from the backend to the attention op.
  • Backend prediction dispatch is shared, while each algorithm implements only
    the hooks it needs.
  • DSA, DeepSeek-V4, and RocketKV are organized by responsibility into
    module.py, backend.py, params.py, metadata.py, cache/indexer helpers,
    and algorithm-owned kernels. SkipSoftmax keeps its existing FMHA integration
    through the shared runtime parameters.

The refactor preserves dense MLA fallback behavior, DSA cross-layer indexer
sharing and piecewise CUDA graph support, DeepSeek-V4 compressed-cache paths,
RocketKV, MiniMax-M3 integration, and SkipSoftmax.

Test Coverage

  • No C/C++ changes relative to main; a native rebuild is not required for
    this refactor.
  • pre-commit on all files changed by this PR.
  • Sparse Attention/MLA forward coverage, including DSA and DeepSeek-V4:
    70 passed.
  • Sparse LLM argument coverage: 7 passed, 245 deselected.
  • DSA pinned block-table staging smoke test on B200.
  • Verified that tests imported Python sources and native bindings from this
    worktree.

PR Checklist

Please review the following before submitting your PR:

  • PR description clearly explains what and why. If using CodeRabbit's summary, please make sure it makes sense.

  • PR Follows TRT-LLM CODING GUIDELINES to the best of your knowledge.

  • Test cases are provided for new code paths (see test instructions)

  • If PR introduces API changes, an appropriate PR label is added - either api-compatible or api-breaking. For api-breaking, include BREAKING in the PR title.

  • Any new dependencies have been scanned for license and vulnerabilities

  • CODEOWNERS updated if ownership changes

  • Documentation updated as needed

  • Update tava architecture diagram if there is a significant design change in PR.

  • The reviewers assigned automatically/manually are appropriate for the PR.

  • Please check this after reviewing the above items as appropriate for this PR.

GitHub Bot Help

To see a list of available CI bot commands, please comment /bot help.

@lfr-0531
lfr-0531 requested a review from heyuhhh April 3, 2026 09:45
@lfr-0531
lfr-0531 force-pushed the user/fanrongl/sparse-attention-refactor branch 8 times, most recently from a1e4402 to eaab4c3 Compare April 3, 2026 11:18
@lfr-0531

lfr-0531 commented Apr 3, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #41657 [ run ] triggered by Bot. Commit: eaab4c3 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #41657 [ run ] completed with state SUCCESS. Commit: eaab4c3
/LLM/main/L0_MergeRequest_PR pipeline #32562 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

Link to invocation

@lfr-0531
lfr-0531 force-pushed the user/fanrongl/sparse-attention-refactor branch 2 times, most recently from 6d89705 to 4004b1a Compare April 4, 2026 06:23
@lfr-0531

lfr-0531 commented Apr 4, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #41813 [ run ] triggered by Bot. Commit: 4004b1a Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #41813 [ run ] completed with state DISABLED
CI server is currently disabled for scheduled maintenance. Estimated completion time: 9 PM PST on 4/4.

Link to invocation

@lfr-0531

lfr-0531 commented Apr 6, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #41921 [ run ] triggered by Bot. Commit: 4004b1a Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #41921 [ run ] completed with state SUCCESS. Commit: 4004b1a
/LLM/main/L0_MergeRequest_PR pipeline #32780 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

Link to invocation

@lfr-0531

lfr-0531 commented Apr 6, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #41962 [ run ] triggered by Bot. Commit: 0a79557 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #41962 [ run ] completed with state SUCCESS. Commit: 0a79557
/LLM/main/L0_MergeRequest_PR pipeline #32816 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

Link to invocation


TLLM_CHECK(host_kv_cache_pool_mapping.has_value());
int32_t const layer_num = host_kv_cache_pool_mapping.value().size(0);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can add an assert sentence here to make sure sparse_mla_topk_value is a valid value

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Comment thread .pre-commit-config.yaml
tensorrt_llm/_torch/attention_backend/sparse/dsa.py |
tensorrt_llm/_torch/attention_backend/sparse/kernel.py |
tensorrt_llm/_torch/attention_backend/sparse/rocket.py |
tensorrt_llm/_torch/attention_backend/sparse/utils.py |

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we just remove there files but not add new files?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The removed files (dsa.py, kernel.py, rocket.py, utils.py) were in the legacy exclusion list because they had historical lint violations that were grandfathered in.

 The new replacement files (dsa/backend.py, dsa/custom_ops.py, dsa/indexer.py, dsa/metadata.py, dsa/cache_manager.py, rocket/backend.py, skip_softmax/backend.py, params.py, etc.) are written from scratch and fully comply with current lint standards — ruff check passes on all of them without any exclusions. So they don't need to be added to the legacy list.

 The same applies to legacy-files.txt, pyproject.toml, and ruff-legacy.toml — all four config files are auto-generated from legacy-files.txt via scripts/legacy_utils.py gen-configs.

@lfr-0531
lfr-0531 force-pushed the user/fanrongl/sparse-attention-refactor branch 3 times, most recently from 79c8913 to 53a6ee5 Compare April 7, 2026 03:07
@lfr-0531

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61504 [ run ] triggered by Bot. Commit: 2568509 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61465 [ run ] completed with state ABORTED. Commit: f605d08

Link to invocation

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
tests/unittest/_torch/attention/sparse/rocketkv/test_kernels.py (1)

9-13: 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Add the new rocketkv tests to the integration lists

  • test_triton_bmm, test_triton_rocket_paged_kt_cache_bmm, and test_topk_kernel are new in tests/unittest/_torch/attention/sparse/rocketkv/test_kernels.py.
  • No matching entries appear under tests/integration/test_lists/test-db/ or tests/integration/test_lists/qa/, so these tests won’t be picked up by the integration harness.

Coverage verdict: insufficient.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/unittest/_torch/attention/sparse/rocketkv/test_kernels.py` around lines
9 - 13, Add integration-list entries for test_triton_bmm,
test_triton_rocket_paged_kt_cache_bmm, and test_topk_kernel from test_kernels.py
in the appropriate test-db and qa lists, using the existing list format so the
integration harness discovers all three tests.

Source: Path instructions

🧹 Nitpick comments (1)
tensorrt_llm/_torch/attention_backend/sparse/rocket/kernels.py (1)

727-728: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Dead no-op expression. Line 728 computes num_kv_heads * num_heads_per_kv but discards the result — it has no effect and looks like a leftover (possibly intended to assign a num_heads/total_heads local). Remove it or assign it if the value is needed.

♻️ Suggested cleanup
     num_gen_tokens, num_kv_heads, num_heads_per_kv, head_dim = q.shape
-    num_kv_heads * num_heads_per_kv
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tensorrt_llm/_torch/attention_backend/sparse/rocket/kernels.py` around lines
727 - 728, Remove the discarded num_kv_heads * num_heads_per_kv expression after
the q.shape unpacking, unless a later computation requires that product; if
needed, assign it to the appropriate existing heads-count local and use that
value.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@tests/unittest/_torch/attention/sparse/rocketkv/test_kernels.py`:
- Around line 9-13: Add integration-list entries for test_triton_bmm,
test_triton_rocket_paged_kt_cache_bmm, and test_topk_kernel from test_kernels.py
in the appropriate test-db and qa lists, using the existing list format so the
integration harness discovers all three tests.

---

Nitpick comments:
In `@tensorrt_llm/_torch/attention_backend/sparse/rocket/kernels.py`:
- Around line 727-728: Remove the discarded num_kv_heads * num_heads_per_kv
expression after the q.shape unpacking, unless a later computation requires that
product; if needed, assign it to the appropriate existing heads-count local and
use that value.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 81865ea1-94a0-455d-88fc-d90fc7d04e41

📥 Commits

Reviewing files that changed from the base of the PR and between f605d08 and 2568509.

📒 Files selected for processing (75)
  • .pre-commit-config.yaml
  • docs/source/blogs/tech_blog/blog17_Sparse_Attention_in_TensorRT-LLM.md
  • docs/source/developer-guide/sparse-attention-development-guide.md
  • docs/source/torch/adding_custom_kernels.md
  • legacy-files.txt
  • pyproject.toml
  • ruff-legacy.toml
  • tensorrt_llm/_torch/attention_backend/fmha/fallback.py
  • tensorrt_llm/_torch/attention_backend/fmha/flashinfer_trtllm_gen.py
  • tensorrt_llm/_torch/attention_backend/fmha/msa_sparse_gqa.py
  • tensorrt_llm/_torch/attention_backend/interface.py
  • tensorrt_llm/_torch/attention_backend/sparse/__init__.py
  • tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/__init__.py
  • tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/backend.py
  • tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/cache_manager.py
  • tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/compressor.py
  • tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/indexer.py
  • tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/kernels.py
  • tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/metadata.py
  • tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/module.py
  • tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/params.py
  • tensorrt_llm/_torch/attention_backend/sparse/dsa/__init__.py
  • tensorrt_llm/_torch/attention_backend/sparse/dsa/backend.py
  • tensorrt_llm/_torch/attention_backend/sparse/dsa/cache_manager.py
  • tensorrt_llm/_torch/attention_backend/sparse/dsa/custom_ops.py
  • tensorrt_llm/_torch/attention_backend/sparse/dsa/indexer.py
  • tensorrt_llm/_torch/attention_backend/sparse/dsa/kernels.py
  • tensorrt_llm/_torch/attention_backend/sparse/dsa/metadata.py
  • tensorrt_llm/_torch/attention_backend/sparse/dsa/module.py
  • tensorrt_llm/_torch/attention_backend/sparse/dsa/params.py
  • tensorrt_llm/_torch/attention_backend/sparse/hooks.py
  • tensorrt_llm/_torch/attention_backend/sparse/minimax_m3/msa_backend.py
  • tensorrt_llm/_torch/attention_backend/sparse/params.py
  • tensorrt_llm/_torch/attention_backend/sparse/registry.py
  • tensorrt_llm/_torch/attention_backend/sparse/rocket.py
  • tensorrt_llm/_torch/attention_backend/sparse/rocket/__init__.py
  • tensorrt_llm/_torch/attention_backend/sparse/rocket/backend.py
  • tensorrt_llm/_torch/attention_backend/sparse/rocket/cache_manager.py
  • tensorrt_llm/_torch/attention_backend/sparse/rocket/kernels.py
  • tensorrt_llm/_torch/attention_backend/sparse/rocket/metadata.py
  • tensorrt_llm/_torch/attention_backend/sparse/rocket/module.py
  • tensorrt_llm/_torch/attention_backend/sparse/rocket/params.py
  • tensorrt_llm/_torch/attention_backend/sparse/skip_softmax/__init__.py
  • tensorrt_llm/_torch/attention_backend/sparse/skip_softmax/params.py
  • tensorrt_llm/_torch/attention_backend/trtllm.py
  • tensorrt_llm/_torch/attention_backend/vanilla.py
  • tensorrt_llm/_torch/models/modeling_deepseekv3.py
  • tensorrt_llm/_torch/models/modeling_deepseekv4.py
  • tensorrt_llm/_torch/models/modeling_minimaxm3.py
  • tensorrt_llm/_torch/modules/ATTENTION_DEVELOPER_GUIDE.md
  • tensorrt_llm/_torch/modules/attention.py
  • tensorrt_llm/_torch/modules/mla.py
  • tensorrt_llm/llmapi/llm_args.py
  • tests/unittest/_torch/attention/sparse/deepseek_v4/test_compressor_module.py
  • tests/unittest/_torch/attention/sparse/deepseek_v4/test_deepseek_v4_cache_manager.py
  • tests/unittest/_torch/attention/sparse/deepseek_v4/test_deepseek_v4_fp4_indexer.py
  • tests/unittest/_torch/attention/sparse/deepseek_v4/test_deepseek_v4_indices_transform.py
  • tests/unittest/_torch/attention/sparse/deepseek_v4/test_deepseek_v4_o_proj.py
  • tests/unittest/_torch/attention/sparse/deepseek_v4/test_deepseek_v4_sparse_mla.py
  • tests/unittest/_torch/attention/sparse/dsa/test_dsa_indexer.py
  • tests/unittest/_torch/attention/sparse/dsa/test_dsa_sparse_mla.py
  • tests/unittest/_torch/attention/sparse/dsa/test_kernels.py
  • tests/unittest/_torch/attention/sparse/dsa/test_short_seq_mha.py
  • tests/unittest/_torch/attention/sparse/rocketkv/test_kernels.py
  • tests/unittest/_torch/attention/sparse/test_minimax_m3_msa_backend.py
  • tests/unittest/_torch/attention/sparse/test_sparse_attention.py
  • tests/unittest/_torch/attention/sparse/test_sparse_mla_forward.py
  • tests/unittest/_torch/attention/sparse/test_triton_topk.py
  • tests/unittest/_torch/attention/test_attention_op_sync.py
  • tests/unittest/_torch/custom_ops/test_deepseek_v4_q_norm.py
  • tests/unittest/_torch/modeling/test_modeling_deepseekv4.py
  • tests/unittest/_torch/modules/test_mla_helix.py
  • tests/unittest/_torch/visual_gen/sparse_attention/test_skip_softmax.py
  • tests/unittest/disaggregated/test_deepseek_v4_kv_transfer.py
  • tests/unittest/llmapi/test_llm_args.py
💤 Files with no reviewable changes (2)
  • tests/unittest/_torch/attention/sparse/test_triton_topk.py
  • tensorrt_llm/_torch/attention_backend/sparse/rocket.py
🚧 Files skipped from review as they are similar to previous changes (56)
  • tensorrt_llm/_torch/attention_backend/sparse/registry.py
  • tensorrt_llm/_torch/attention_backend/sparse/rocket/module.py
  • tensorrt_llm/_torch/attention_backend/sparse/rocket/init.py
  • tensorrt_llm/_torch/attention_backend/sparse/init.py
  • tensorrt_llm/_torch/models/modeling_minimaxm3.py
  • tensorrt_llm/_torch/attention_backend/sparse/rocket/params.py
  • tensorrt_llm/_torch/attention_backend/sparse/skip_softmax/init.py
  • tests/unittest/_torch/attention/sparse/dsa/test_kernels.py
  • pyproject.toml
  • tests/unittest/disaggregated/test_deepseek_v4_kv_transfer.py
  • tensorrt_llm/llmapi/llm_args.py
  • tensorrt_llm/_torch/attention_backend/sparse/dsa/init.py
  • legacy-files.txt
  • tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/compressor.py
  • ruff-legacy.toml
  • tests/unittest/_torch/attention/sparse/deepseek_v4/test_deepseek_v4_indices_transform.py
  • tests/unittest/_torch/attention/test_attention_op_sync.py
  • tensorrt_llm/_torch/attention_backend/sparse/minimax_m3/msa_backend.py
  • docs/source/torch/adding_custom_kernels.md
  • tests/unittest/_torch/attention/sparse/deepseek_v4/test_deepseek_v4_o_proj.py
  • tensorrt_llm/_torch/attention_backend/fmha/flashinfer_trtllm_gen.py
  • tests/unittest/_torch/modules/test_mla_helix.py
  • tests/unittest/_torch/attention/sparse/deepseek_v4/test_compressor_module.py
  • tests/unittest/_torch/visual_gen/sparse_attention/test_skip_softmax.py
  • tensorrt_llm/_torch/attention_backend/vanilla.py
  • tensorrt_llm/_torch/models/modeling_deepseekv4.py
  • docs/source/blogs/tech_blog/blog17_Sparse_Attention_in_TensorRT-LLM.md
  • tensorrt_llm/_torch/attention_backend/sparse/dsa/backend.py
  • tests/unittest/_torch/attention/sparse/dsa/test_dsa_indexer.py
  • tests/unittest/_torch/attention/sparse/deepseek_v4/test_deepseek_v4_cache_manager.py
  • tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/cache_manager.py
  • tests/unittest/_torch/modeling/test_modeling_deepseekv4.py
  • tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/params.py
  • tensorrt_llm/_torch/modules/ATTENTION_DEVELOPER_GUIDE.md
  • docs/source/developer-guide/sparse-attention-development-guide.md
  • tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/init.py
  • tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/indexer.py
  • tensorrt_llm/_torch/attention_backend/sparse/hooks.py
  • tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/metadata.py
  • tensorrt_llm/_torch/attention_backend/trtllm.py
  • tensorrt_llm/_torch/attention_backend/fmha/fallback.py
  • tensorrt_llm/_torch/attention_backend/sparse/dsa/cache_manager.py
  • tensorrt_llm/_torch/attention_backend/sparse/skip_softmax/params.py
  • tensorrt_llm/_torch/attention_backend/sparse/params.py
  • tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/backend.py
  • tensorrt_llm/_torch/attention_backend/sparse/dsa/params.py
  • tensorrt_llm/_torch/attention_backend/sparse/rocket/metadata.py
  • tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/module.py
  • tensorrt_llm/_torch/modules/attention.py
  • tests/unittest/_torch/attention/sparse/dsa/test_short_seq_mha.py
  • tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/kernels.py
  • tests/unittest/_torch/attention/sparse/deepseek_v4/test_deepseek_v4_sparse_mla.py
  • tensorrt_llm/_torch/attention_backend/sparse/dsa/module.py
  • tensorrt_llm/_torch/attention_backend/sparse/dsa/indexer.py
  • tests/unittest/_torch/attention/sparse/test_sparse_mla_forward.py
  • tensorrt_llm/_torch/modules/mla.py

@lfr-0531

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (2)
tensorrt_llm/_torch/attention_backend/sparse/rocket/kernels.py (1)

727-728: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Dead expression statement. num_kv_heads * num_heads_per_kv computes and discards a value with no effect. Remove it (or assign it if it was meant to be used).

♻️ Proposed cleanup
     num_gen_tokens, num_kv_heads, num_heads_per_kv, head_dim = q.shape
-    num_kv_heads * num_heads_per_kv
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tensorrt_llm/_torch/attention_backend/sparse/rocket/kernels.py` around lines
727 - 728, Remove the unused expression statement `num_kv_heads *
num_heads_per_kv` from the code immediately following the q.shape unpacking;
leave the existing shape assignments unchanged unless the computed value is
required by subsequent logic.
tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/indexer.py (1)

102-105: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Missing return-type annotations.

post_load_weights (Line 102), _qk_projection_and_rope (Line 107), and forward (Line 357) lack return-type annotations (-> None / -> torch.Tensor).

As per coding guidelines: "Annotate every function, use None for non-returning functions."

✏️ Proposed fix
-    def post_load_weights(self):
+    def post_load_weights(self) -> None:
         # V4 does not use the V3 fused fp32 wk+weights_proj GEMM, and the
         # base concat would now hit an fp32/bf16 dtype mismatch.
         return
-    def _qk_projection_and_rope(self, qr: torch.Tensor, position_ids: torch.Tensor):
+    def _qk_projection_and_rope(self, qr: torch.Tensor, position_ids: torch.Tensor) -> torch.Tensor:
     def forward(
         self,
         qr: torch.Tensor,
         hidden_states: torch.Tensor,
         metadata: DeepseekV4TrtllmAttentionMetadata,
         position_ids: torch.Tensor,
         pre_aux: Optional[
             Tuple[torch.Tensor, Optional[torch.Tensor], Optional[torch.Tensor]]
         ] = None,
-    ):
+    ) -> torch.Tensor:

Also applies to: 107-115, 357-366

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/indexer.py` around
lines 102 - 105, Annotate post_load_weights with -> None,
_qk_projection_and_rope with -> torch.Tensor, and forward with the appropriate
torch.Tensor return annotation, preserving their existing behavior and
signatures otherwise.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/indexer.py`:
- Around line 208-224: The FP8_BLOCKWISE path in _update_k_cache_if_needed
currently invokes _update_k_cache even though that override is a no-op, leaving
the indexer K cache unpopulated. Implement the required FP8 blockwise cache
update so this path populates the cache, or remove the unreachable FP8 handling
including the k_scale assertion if FP8 blockwise is unsupported; keep the MXFP4
early return unchanged.

In `@tests/unittest/_torch/custom_ops/test_deepseek_v4_q_norm.py`:
- Around line 221-226: The test currently passes a flattened rank-2 view to
deepseek_v4_q_norm_fused_fp8 instead of exercising the documented 3D q_pe
contract. Update the invocation around q_pe and the custom op to pass the
original 3D tensor, or validate it through the sparse-MLA attention consumer,
while retaining assertions that verify the actual consumer path rather than only
the allocation.

---

Nitpick comments:
In `@tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/indexer.py`:
- Around line 102-105: Annotate post_load_weights with -> None,
_qk_projection_and_rope with -> torch.Tensor, and forward with the appropriate
torch.Tensor return annotation, preserving their existing behavior and
signatures otherwise.

In `@tensorrt_llm/_torch/attention_backend/sparse/rocket/kernels.py`:
- Around line 727-728: Remove the unused expression statement `num_kv_heads *
num_heads_per_kv` from the code immediately following the q.shape unpacking;
leave the existing shape assignments unchanged unless the computed value is
required by subsequent logic.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: d21421a4-1c83-4620-bfaa-d0675f36744a

📥 Commits

Reviewing files that changed from the base of the PR and between 2568509 and b6789ad.

📒 Files selected for processing (75)
  • .pre-commit-config.yaml
  • docs/source/blogs/tech_blog/blog17_Sparse_Attention_in_TensorRT-LLM.md
  • docs/source/developer-guide/sparse-attention-development-guide.md
  • docs/source/torch/adding_custom_kernels.md
  • legacy-files.txt
  • pyproject.toml
  • ruff-legacy.toml
  • tensorrt_llm/_torch/attention_backend/fmha/fallback.py
  • tensorrt_llm/_torch/attention_backend/fmha/flashinfer_trtllm_gen.py
  • tensorrt_llm/_torch/attention_backend/fmha/msa_sparse_gqa.py
  • tensorrt_llm/_torch/attention_backend/interface.py
  • tensorrt_llm/_torch/attention_backend/sparse/__init__.py
  • tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/__init__.py
  • tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/backend.py
  • tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/cache_manager.py
  • tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/compressor.py
  • tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/indexer.py
  • tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/kernels.py
  • tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/metadata.py
  • tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/module.py
  • tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/params.py
  • tensorrt_llm/_torch/attention_backend/sparse/dsa/__init__.py
  • tensorrt_llm/_torch/attention_backend/sparse/dsa/backend.py
  • tensorrt_llm/_torch/attention_backend/sparse/dsa/cache_manager.py
  • tensorrt_llm/_torch/attention_backend/sparse/dsa/custom_ops.py
  • tensorrt_llm/_torch/attention_backend/sparse/dsa/indexer.py
  • tensorrt_llm/_torch/attention_backend/sparse/dsa/kernels.py
  • tensorrt_llm/_torch/attention_backend/sparse/dsa/metadata.py
  • tensorrt_llm/_torch/attention_backend/sparse/dsa/module.py
  • tensorrt_llm/_torch/attention_backend/sparse/dsa/params.py
  • tensorrt_llm/_torch/attention_backend/sparse/hooks.py
  • tensorrt_llm/_torch/attention_backend/sparse/minimax_m3/msa_backend.py
  • tensorrt_llm/_torch/attention_backend/sparse/params.py
  • tensorrt_llm/_torch/attention_backend/sparse/registry.py
  • tensorrt_llm/_torch/attention_backend/sparse/rocket.py
  • tensorrt_llm/_torch/attention_backend/sparse/rocket/__init__.py
  • tensorrt_llm/_torch/attention_backend/sparse/rocket/backend.py
  • tensorrt_llm/_torch/attention_backend/sparse/rocket/cache_manager.py
  • tensorrt_llm/_torch/attention_backend/sparse/rocket/kernels.py
  • tensorrt_llm/_torch/attention_backend/sparse/rocket/metadata.py
  • tensorrt_llm/_torch/attention_backend/sparse/rocket/module.py
  • tensorrt_llm/_torch/attention_backend/sparse/rocket/params.py
  • tensorrt_llm/_torch/attention_backend/sparse/skip_softmax/__init__.py
  • tensorrt_llm/_torch/attention_backend/sparse/skip_softmax/params.py
  • tensorrt_llm/_torch/attention_backend/trtllm.py
  • tensorrt_llm/_torch/attention_backend/vanilla.py
  • tensorrt_llm/_torch/models/modeling_deepseekv3.py
  • tensorrt_llm/_torch/models/modeling_deepseekv4.py
  • tensorrt_llm/_torch/models/modeling_minimaxm3.py
  • tensorrt_llm/_torch/modules/ATTENTION_DEVELOPER_GUIDE.md
  • tensorrt_llm/_torch/modules/attention.py
  • tensorrt_llm/_torch/modules/mla.py
  • tensorrt_llm/llmapi/llm_args.py
  • tests/unittest/_torch/attention/sparse/deepseek_v4/test_compressor_module.py
  • tests/unittest/_torch/attention/sparse/deepseek_v4/test_deepseek_v4_cache_manager.py
  • tests/unittest/_torch/attention/sparse/deepseek_v4/test_deepseek_v4_fp4_indexer.py
  • tests/unittest/_torch/attention/sparse/deepseek_v4/test_deepseek_v4_indices_transform.py
  • tests/unittest/_torch/attention/sparse/deepseek_v4/test_deepseek_v4_o_proj.py
  • tests/unittest/_torch/attention/sparse/deepseek_v4/test_deepseek_v4_sparse_mla.py
  • tests/unittest/_torch/attention/sparse/dsa/test_dsa_indexer.py
  • tests/unittest/_torch/attention/sparse/dsa/test_dsa_sparse_mla.py
  • tests/unittest/_torch/attention/sparse/dsa/test_kernels.py
  • tests/unittest/_torch/attention/sparse/dsa/test_short_seq_mha.py
  • tests/unittest/_torch/attention/sparse/rocketkv/test_kernels.py
  • tests/unittest/_torch/attention/sparse/test_minimax_m3_msa_backend.py
  • tests/unittest/_torch/attention/sparse/test_sparse_attention.py
  • tests/unittest/_torch/attention/sparse/test_sparse_mla_forward.py
  • tests/unittest/_torch/attention/sparse/test_triton_topk.py
  • tests/unittest/_torch/attention/test_attention_op_sync.py
  • tests/unittest/_torch/custom_ops/test_deepseek_v4_q_norm.py
  • tests/unittest/_torch/modeling/test_modeling_deepseekv4.py
  • tests/unittest/_torch/modules/test_mla_helix.py
  • tests/unittest/_torch/visual_gen/sparse_attention/test_skip_softmax.py
  • tests/unittest/disaggregated/test_deepseek_v4_kv_transfer.py
  • tests/unittest/llmapi/test_llm_args.py
💤 Files with no reviewable changes (2)
  • tests/unittest/_torch/attention/sparse/test_triton_topk.py
  • tensorrt_llm/_torch/attention_backend/sparse/rocket.py
🚧 Files skipped from review as they are similar to previous changes (54)
  • tensorrt_llm/_torch/attention_backend/sparse/rocket/init.py
  • tests/unittest/_torch/attention/sparse/deepseek_v4/test_deepseek_v4_indices_transform.py
  • tensorrt_llm/_torch/attention_backend/sparse/rocket/module.py
  • tensorrt_llm/_torch/attention_backend/sparse/registry.py
  • ruff-legacy.toml
  • docs/source/torch/adding_custom_kernels.md
  • tests/unittest/_torch/attention/sparse/deepseek_v4/test_deepseek_v4_cache_manager.py
  • tensorrt_llm/_torch/attention_backend/sparse/skip_softmax/init.py
  • tensorrt_llm/_torch/attention_backend/fmha/msa_sparse_gqa.py
  • tensorrt_llm/_torch/attention_backend/sparse/rocket/params.py
  • tests/unittest/_torch/attention/sparse/test_minimax_m3_msa_backend.py
  • tensorrt_llm/_torch/attention_backend/sparse/init.py
  • tensorrt_llm/_torch/attention_backend/fmha/flashinfer_trtllm_gen.py
  • pyproject.toml
  • tests/unittest/_torch/visual_gen/sparse_attention/test_skip_softmax.py
  • .pre-commit-config.yaml
  • tests/unittest/disaggregated/test_deepseek_v4_kv_transfer.py
  • tests/unittest/_torch/attention/sparse/rocketkv/test_kernels.py
  • tensorrt_llm/_torch/models/modeling_minimaxm3.py
  • tests/unittest/_torch/attention/sparse/deepseek_v4/test_compressor_module.py
  • tests/unittest/_torch/attention/test_attention_op_sync.py
  • tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/compressor.py
  • tensorrt_llm/_torch/models/modeling_deepseekv4.py
  • tests/unittest/_torch/attention/sparse/deepseek_v4/test_deepseek_v4_o_proj.py
  • tests/unittest/_torch/modeling/test_modeling_deepseekv4.py
  • tests/unittest/llmapi/test_llm_args.py
  • tensorrt_llm/_torch/attention_backend/vanilla.py
  • tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/params.py
  • tensorrt_llm/_torch/attention_backend/sparse/hooks.py
  • tensorrt_llm/_torch/attention_backend/sparse/minimax_m3/msa_backend.py
  • tensorrt_llm/llmapi/llm_args.py
  • tests/unittest/_torch/modules/test_mla_helix.py
  • tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/cache_manager.py
  • tests/unittest/_torch/attention/sparse/dsa/test_dsa_sparse_mla.py
  • tensorrt_llm/_torch/modules/ATTENTION_DEVELOPER_GUIDE.md
  • tensorrt_llm/_torch/attention_backend/trtllm.py
  • tensorrt_llm/_torch/attention_backend/sparse/dsa/params.py
  • tensorrt_llm/_torch/attention_backend/fmha/fallback.py
  • tensorrt_llm/_torch/attention_backend/interface.py
  • tensorrt_llm/_torch/attention_backend/sparse/dsa/backend.py
  • tests/unittest/_torch/attention/sparse/dsa/test_short_seq_mha.py
  • tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/backend.py
  • tensorrt_llm/_torch/attention_backend/sparse/dsa/cache_manager.py
  • tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/metadata.py
  • tensorrt_llm/_torch/modules/attention.py
  • tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/module.py
  • tensorrt_llm/_torch/attention_backend/sparse/dsa/indexer.py
  • tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/init.py
  • tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/kernels.py
  • tensorrt_llm/_torch/attention_backend/sparse/rocket/metadata.py
  • tests/unittest/_torch/attention/sparse/test_sparse_attention.py
  • tests/unittest/_torch/attention/sparse/test_sparse_mla_forward.py
  • tensorrt_llm/_torch/attention_backend/sparse/dsa/module.py
  • tensorrt_llm/_torch/modules/mla.py

Comment thread tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/indexer.py
Comment thread tests/unittest/_torch/custom_ops/test_deepseek_v4_q_norm.py
@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61533 [ run ] triggered by Bot. Commit: b6789ad Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61504 [ run ] completed with state ABORTED. Commit: 2568509

Link to invocation

@lfr-0531

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61541 [ run ] triggered by Bot. Commit: b832a2a Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61533 [ run ] completed with state ABORTED. Commit: b6789ad

Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61541 [ run ] completed with state SUCCESS. Commit: b832a2a
/LLM/main/L0_MergeRequest_PR pipeline #49752 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

lfr-0531 added 11 commits July 26, 2026 03:47
Signed-off-by: Fanrong Li <23290157+lfr-0531@users.noreply.github.com>
Signed-off-by: Fanrong Li <23290157+lfr-0531@users.noreply.github.com>
Signed-off-by: Fanrong Li <23290157+lfr-0531@users.noreply.github.com>
Define validated sparse-attention hooks shared by Attention and MLA while
keeping algorithm-specific module behavior under each sparse backend.
Simplify DSA and DeepSeek-V4 module integration, move Rocket kernels into
its backend directory, and align the related architecture tests.

Signed-off-by: Fanrong Li <23290157+lfr-0531@users.noreply.github.com>
Unify module-to-backend and backend-to-attention-op sparse runtime arguments. Split DeepSeek-V4 indexer, metadata, and parameter definitions, and route DSA prediction through the backend while preserving shared TopK buffer lifetime.

Signed-off-by: Fanrong Li <23290157+lfr-0531@users.noreply.github.com>
Allocate a single mixed-batch TopK buffer only when the model contains shared indexer layers. Reuse per-layer indexer routing to derive the metadata requirement and keep the buffer address stable across steps.

Signed-off-by: Fanrong Li <23290157+lfr-0531@users.noreply.github.com>
Keep split DSA indexer test paths under inference mode, matching the original integrated Indexer.forward contract.

Signed-off-by: Fanrong Li <23290157+lfr-0531@users.noreply.github.com>
Signed-off-by: Fanrong Li <23290157+lfr-0531@users.noreply.github.com>
Signed-off-by: Fanrong Li <23290157+lfr-0531@users.noreply.github.com>
Restore the shared lint configuration to match main and format the split sparse attention modules with Ruff.

Signed-off-by: Fanrong Li <23290157+lfr-0531@users.noreply.github.com>
Signed-off-by: Fanrong Li <23290157+lfr-0531@users.noreply.github.com>
@lfr-0531

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61736 [ run ] triggered by Bot. Commit: 20a7e1e Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61736 [ run ] completed with state FAILURE. Commit: 20a7e1e
/LLM/main/L0_MergeRequest_PR pipeline #49940 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

@lfr-0531

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61744 [ run ] triggered by Bot. Commit: 20a7e1e Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61744 [ run ] completed with state FAILURE. Commit: 20a7e1e
/LLM/main/L0_MergeRequest_PR pipeline #49946 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api-compatible Accepted LLM API contract change that is backwards-compatible

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants